package com.onepower.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onepower.core.domain.request.CreateMatchForm;
import com.onepower.core.modules.repository.entity.TkdMatch;
import com.onepower.core.modules.service.ITkdMatchService;
import com.onepower.core.result.CommonResult;
import com.onepower.core.utils.DateUtils;
import com.onepower.core.utils.ResultUtils;
import com.onepower.core.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 比赛表 前端控制器
 * </p>
 *
 * @author kin
 * @since 2018-08-06
 */
@RestController
@RequestMapping("/api/match")
public class MatchController {


    @Autowired
    private ITkdMatchService tkdMatchService;

    @Autowired
    HttpServletRequest request;


    @RequestMapping(value = "create", method = RequestMethod.POST)
    public CommonResult create(@RequestBody CreateMatchForm form) {
        form.setCreaterId(SecurityUtils.getCurrentUserId(request));
        if (StringUtils.isNotEmpty(form.getMatchTime())) {
            form.setMatchDate(DateUtils.stringToDate(form.getMatchTime()));
        }
        return tkdMatchService.createMatch(form);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public CommonResult update(@RequestBody CreateMatchForm form) {
        tkdMatchService.updateMatch(form);
        return ResultUtils.ok();
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(value = "matchName", required = false) String matchName,
                             @RequestParam(value = "matchTime", required = false) Date matchTime,
                             @RequestParam(value = "multiType", required = false) Boolean multiType,
                             @RequestParam(value = "pullData", required = false) Boolean pullData,
                             @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "20") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        if (matchName != null && !StringUtils.isEmpty(matchName)) {
            map.put("matchName", matchName);
        }
        if (matchTime != null) {
            map.put("matchTime", matchName);
        }
        if (multiType != null) {
            map.put("multiType", multiType);
        }
        if (pullData != null) {
            map.put("pullData", pullData);
        }
        Page<TkdMatch> matchList = tkdMatchService.getList(pageNum, pageSize, map);
        return ResultUtils.ok(matchList);
    }

    @RequestMapping(value = "info", method = RequestMethod.GET)
    public CommonResult getMatchInfo(@RequestParam(value = "matchId", required = false, defaultValue = "-1") Integer matchId) {
        return ResultUtils.ok(tkdMatchService.getMatchInfo(matchId));
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public CommonResult delete(@RequestBody Integer[] matchIds) {
        tkdMatchService.deleteMatch(Arrays.asList(matchIds));
        return ResultUtils.ok();
    }
}
